package cc.kigh.oss.service.abstractd;

import cc.kigh.oss.entity.UploadResult;
import cc.kigh.oss.service.IOssStrategy;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cc.kigh.common.utils.DateUtils;
import cc.kigh.common.utils.StringUtils;
import cc.kigh.oss.properties.OssProperties;

import java.io.InputStream;

/**
 * 对象存储策略(支持七牛、阿里云、腾讯云、minio)
 *
 * @author Lion Li
 */
public abstract class AbstractOssStrategy implements IOssStrategy {

    protected OssProperties properties;

    /**
     * 配置初始化
     * @param properties
     */
    public abstract void init(OssProperties properties);


    public String getPath(String prefix, String suffix) {
        // 生成uuid
        String uuid = IdUtil.fastSimpleUUID();
        // 文件路径
        String path = DateUtils.datePath() + "/" + uuid;
        if (StringUtils.isNotBlank(prefix)) {
            path = prefix + "/" + path;
        }
        return path + suffix;
    }


    @Override
    public UploadResult upload(InputStream inputStream, String path, String contentType, boolean privateAccess) {
        byte[] data = IoUtil.readBytes(inputStream);
        return this.upload(data, path, contentType, false);
    }


    /**
     * 获取节点地址
     * @return 获取节点地址
     */
    public abstract String getEndpointLink();
}
